🎵 Jingle Bell Button Game
We created this activity using ChatGPT, an AI tool. We've not had time to test it, so it might not work correctly. If it doesn't quite work, try to fix it if you can!Welcome! In this project, you’ll build a fun Christmas guessing game using your Raspberry Pi Pico. When you press the button, a random Christmas tune will play, and everyone can guess which tune it is! 🎄
Step 1: Hardware Setup
Let’s start by connecting all the components to your Raspberry Pi Pico. We’ll use jumper wires to directly connect the components.
1. Components You’ll Need
- Raspberry Pi Pico with headers
- Passive Buzzer
- Push Button
- 5x Jumper Wires (Male-to-Male)
2. Connecting the Components
Connect the Buzzer:
- Take the long leg (positive) of the buzzer and connect it to GPIO 15 on the Pico using a jumper wire.
- Take the short leg (negative) of the buzzer and connect it to GND on the Pico.
Connect the Push Button:
- Connect one leg of the push button to GPIO 14 on the Pico.
- Connect the other leg of the push button to GND on the Pico.
3. Powering the Circuit
Once all connections are in place, plug your Raspberry Pi Pico into your computer using a USB cable. This will power the Pico and the components.
🎉 Great job! You’ve wired up your buzzer and button correctly. Now we’re ready to code and bring the project to life!
Step 2: Writing the Code
Now we’ll program the Raspberry Pi Pico to play random Christmas tunes when the button is pressed. Follow the step-by-step instructions carefully, and don’t forget to test your project as you go!
1. Import Libraries
Type the following code to import the libraries needed for controlling the GPIO pins and generating random tunes:
What does this do?
machine
: Lets you control the Pico’s pins.utime
: Allows delays in your program.random
: Used to pick a random Christmas tune.
✨ Nice start! You’ve imported everything needed to work with the Pico and play random tunes.
2. Set Up the Pins
Next, set up the GPIO pins for the buzzer and button:
What does this do?
buzzer
: Configures GPIO 15 as an output pin for the buzzer.button
: Configures GPIO 14 as an input pin for the button, with a pull-down resistor.
🎉 Excellent! Your Pico now knows which pins are connected to the button and buzzer.
3. Define the Core Christmas Tunes
We’ll store Christmas tunes in a **dictionary**, a type of array that uses a key-value pair. Each key is the tune’s name, and the value is a list of notes and durations. Here’s how:
Explanation:
- Dictionary: Allows us to group multiple tunes in one place.
- Key: The name of the tune (e.g., "Jingle Bells").
- Value: A list of tuples, where each tuple contains a frequency and a duration.
🎶 Fantastic! You’ve set up your first tunes. Let’s write code to play them!
Step 3: Write the Function to Play a Tune
Write this function to play a melody:
Explanation:
- The function loops through the list of notes and durations.
buzzer.freq(note)
: Sets the buzzer’s frequency to the note.utime.sleep(duration)
: Plays each note for the specified duration.
🎵 Great! You’re ready to use the button to play a random tune!
Step 4: Randomly Play a Tune
Now let’s make the button play a random tune when pressed:
Explanation:
button.value() == 1
: Checks if the button is pressed.random.choice()
: Picks a random key (tune name) from the dictionary.play_tune()
: Plays the tune corresponding to the random key.
🎉 Awesome! You can now play random tunes when you press the button!
Step 5: Add More Christmas Tunes
Want to expand your project? Add these tunes to your dictionary:
Explanation:
- Adding a new key-value pair to a dictionary is like appending to an array.
- Use the tune name as the key, and its notes and durations as the value.
🎶 Amazing work! Keep adding tunes and make your project even more festive!